package cn.hxth.cloud.datasync.rabbitmq;

import cn.hxth.cloud.api.domain.ProductOutputOrder;
import cn.hxth.cloud.api.domain.SteelsOutputOrder;
import cn.hxth.cloud.api.rabbitmq.AbstractRabbitMQHandler;
import cn.hxth.cloud.api.rabbitmq.RabbitMQSender;
import cn.hxth.cloud.service.service.InventoryService;
import cn.hxth.cloud.service.service.OrderService;
import cn.hxth.cloud.service.service.ProductionService;
import cn.hxth.cloud.service.service.WmsService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "data-change-queue-wms")
public class DataChangeQueueReceiver extends AbstractRabbitMQHandler {


    @Autowired
    private OrderService orderService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private ProductionService productionService;
    @Autowired
    private WmsService wmsService;
    /*@Resource
    private WmsFacadeService wmsFacadeService ;*/
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    RabbitMQSender rabbitMQSender ;

    public DataChangeQueueReceiver(RabbitMQSender rabbitMQSender) {
        super(rabbitMQSender);
        super.setLogger(DataChangeQueueReceiver.class);
    }


    private Boolean processInsertSteelsOutputOrder(JSONObject jsonObject, String jsonData) throws Exception {
        SteelsOutputOrder steelsOutputOrder = JSONObject.parseObject(jsonData, SteelsOutputOrder.class);
        Boolean flag = wmsService.insertSteelsOutputOrder(steelsOutputOrder);
        if(flag){
            this.redisTemplate.delete("steelsOutputOrder_"+steelsOutputOrder.getPid()) ;
        }

        // ProductionOrder productionOrder = steelsOutputOrder.getProductionOrder();

        return  flag ;
    }

    private Boolean processInsertProductOutputOrder(JSONObject jsonObject, String JsonData) throws Exception {

        ProductOutputOrder productOutputOrder = JSONObject.parseObject(JsonData, ProductOutputOrder.class);
        boolean flag = this.wmsService.insertProductOutputOrder(productOutputOrder);
       return flag ;

    }
}
